from jili.data.db import getdb_client,insert_one
from jili.tool.convert import str2datetime
def get_flag_date(name,ip="127.0.0.1",auth="ant:ant",db="stock"):
    db=getdb_client(dbname=db,ip=ip,auth=auth)
    cu=db["flagdate_info"]
    i=cu.find_one({"name":name})
    db.client.close()
    if i:
        return i["date"]
    else:
        return None

def set_flag_date(name,date,ip,auth,db):
    db = getdb_client(dbname=db, ip=ip, auth=auth)
    cu = db["flagdate_info"]
    n=cu.count_documents({})
    if n==0:
        cu.create_index([("name", 1)], name="name1", unique=False)
    i = cu.find_one({"name": name})
    tk=str2datetime(date)
    if i:
        i["date"]=tk
        if tk not in i["log"]:
            i["log"].append(tk)
        insert_one(cu,i,updteflag=True)
    else:
        cu.insert_one({"name":name,"date":tk,"log":[tk]})
    db.client.close()


if __name__=="__main__":
    set_flag_date("test","20230401")
    print(get_flag_date("test"))
    set_flag_date("test", "20230402")